﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Engine.GameStateManagement;
using Engine.Physics;

namespace Engine
{

    public class Engine
    {
        /// <summary>
        /// A global reference to the current active game.
        /// </summary>
        public Game Game { get; private set; }
        public ScreenManager ScreenManager { get; private set; }
        public GraphicsResources Graphics { get; private set; }

        public List<Manager> Managers { get; private set; }

        #region Static Fields
        static Engine _instance;
        public static Engine Instance { get { return _instance; } }

        #endregion

        #region Initialize

        public Engine()
        {
            Managers = new List<Manager>(4);
        }

        public void Initialize(Game game)
        {
            //Initialize the managers.
            this.Game = game;

            //Initialize Screen Manager (FIX).
            ScreenManager = new ScreenManager(game);
            game.Components.Add(ScreenManager);
            //Managers.Add(new ScreenManager(game));

            //Initialize InputManager
            Managers.Add(new InputManager());

            //Collision Solver
            Managers.Add(new CollisionSolver());

            //Physics Manager
            Managers.Add(new PhysicsManager());

            //Initialize Graphics Resources
            Graphics = new GraphicsResources(game.GraphicsDevice);
        }

        #endregion

        #region Update

        public virtual void Update(GameTime gameTime)
        {
            foreach (var m in Managers)
            {
                if (m.Enabled)
                    m.Update(gameTime);
            }
        }

        #endregion

        #region Static Constructor

        static Engine()
        {
            _instance = new Engine();
        }

        #endregion

    }

    public class GraphicsResources
    {
        public SpriteBatch SpriteBatch { get; private set; }

        public GraphicsResources(GraphicsDevice device)
        {
            SpriteBatch = new SpriteBatch(device);
        }
    }
}
